/************************************************************************
 * 描述 ：数据库表WMS_MN对应的Service，代码生成。
 * 作者 ：wms_admin
 * 日期 ：2019-08-08 14:26:52
 *
 ************************************************************************/

package com.pangus.ims.wms.service;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aspose.slides.p2cbca448.ifd;
import com.pangus.PangusConstants;
import com.pangus.common.dto.EditDataResult;
import com.pangus.common.dto.EditResult;
import com.pangus.common.dto.JqGridRequest;
import com.pangus.common.dto.JqGridResponse;
import com.pangus.common.dto.JsonRequest;
import com.pangus.common.excel.CheckExcelResult;
import com.pangus.common.excel.callback.ExcelCallbackCommonService;
import com.pangus.common.sys.dto.domain.ConfigDto;
import com.pangus.common.sys.dto.domain.LogDto;
import com.pangus.common.sys.service.CompanyService;
import com.pangus.common.sys.service.ConfigService;
import com.pangus.common.sys.service.DataRolePointService;
import com.pangus.common.sys.service.ParameterService;
import com.pangus.common.sys.utils.CheckUtils;
import com.pangus.common.sys.utils.CommonUtils;
import com.pangus.common.sys.utils.FileUploadUtils;
import com.pangus.common.sys.utils.MsgUtils;
import com.pangus.common.sys.utils.ParamUtils;
import com.pangus.common.sys.utils.PartnerCodeHolder;
import com.pangus.common.sys.utils.SqlQueryUtils;
import com.pangus.common.sys.utils.SysContextHolder;
import com.pangus.error.exception.BaseException;
import com.pangus.error.exception.ValueEmptyException;
import com.pangus.fa.i18n.FaMessageSource;
import com.pangus.fa.mybatis.mapper.entity.Condition;
import com.pangus.fa.mybatis.mapper.entity.Condition.Criteria;
import com.pangus.fa.service.BaseService;
import com.pangus.fa.util.DateUtils;
import com.pangus.fa.util.FastIdUtils;
import com.pangus.fa.util.StringUtils;
import com.pangus.ims.bs.BsConstants.BsMnMtrl.NOT_MERGE;
import com.pangus.ims.bs.BsConstants.BsMoMtrlloc.IS_MAIN;
import com.pangus.ims.bs.BsConstants.IsBonded;
import com.pangus.ims.bs.BsConstants.IsOutQc;
import com.pangus.ims.bs.BsConstants.IsValid;
import com.pangus.ims.bs.BsConstants.MtrlLev;
import com.pangus.ims.bs.BsConstants.RcvStat;
import com.pangus.ims.bs.BsConstants.RcvType;
import com.pangus.ims.bs.dto.BsCodeHdFormDto;
import com.pangus.ims.bs.dto.domain.BsMnLpnDto;
import com.pangus.ims.bs.dto.domain.BsMnMtrlDto;
import com.pangus.ims.bs.dto.domain.BsMnOptsDto;
import com.pangus.ims.bs.dto.domain.BsMtrlDto;
import com.pangus.ims.bs.dto.domain.BsStoreDto;
import com.pangus.ims.bs.form.BsMnLpnForm;
import com.pangus.ims.bs.form.BsMnMtrlForm;
import com.pangus.ims.bs.form.BsMnOptsForm;
import com.pangus.ims.bs.form.BsMtrlForm;
import com.pangus.ims.bs.service.BsCodeHdService;
import com.pangus.ims.bs.service.BsMnLpnService;
import com.pangus.ims.bs.service.BsMnMtrlService;
import com.pangus.ims.bs.service.BsMnOptsService;
import com.pangus.ims.bs.service.BsMtrlService;
import com.pangus.ims.bs.service.BsStoreService;
import com.pangus.ims.elec.ElecConstants;
import com.pangus.ims.elec.utils.ElecCommonUtils;
import com.pangus.ims.wms.WmsConstants;
import com.pangus.ims.wms.WmsConstants.MN_AUDIT_STAT;
import com.pangus.ims.wms.WmsConstants.MnIoType;
import com.pangus.ims.wms.WmsConstants.OmtrlType;
import com.pangus.ims.wms.WmsConstants.WmsOutStockStrategy;
import com.pangus.ims.wms.WmsConstants.WmsPickingStrategy;
import com.pangus.ims.wms.dto.domain.WmsBsMtrlDto;
import com.pangus.ims.wms.dto.domain.WmsMnDto;
import com.pangus.ims.wms.form.WmsBsMtrlForm;
import com.pangus.ims.wms.form.WmsMnForm;
import com.pangus.ims.wms.utils.PdfCreateUtil;
import com.pangus.ims.wms.utils.WmsCommonUtils;
import com.thoughtworks.xstream.alias.ClassMapper.Null;

@Service
public class WmsMnBadService extends BaseService<WmsMnDto> implements ExcelCallbackCommonService<WmsMnForm> {
	
	private final Logger logger = LoggerFactory.getLogger(WmsMnBadService.class);

	@Autowired
	private FaMessageSource messageSource;

	@Autowired
	private ParameterService parameterService;

	@Autowired
	private DataRolePointService dataRolePointService;

	@Autowired
	private CompanyService companyService;

	@Autowired
	private SqlQueryUtils sqlQueryUtils;

	@Autowired
	private FastIdUtils fastIdUtils;

	@Autowired
	private ConfigService configService;

	@Autowired
	private BsStoreService bsStoreService;

	@Autowired
	private BsCodeHdService bsCodeHdService;

	@Autowired
	private BsMnOptsService bsMnOptsService;

	@Autowired
	private BsMnMtrlService bsMnMtrlService;

	@Autowired
	private WmsBsMtrlService wmsBsMtrlService;

	@Autowired
	private BsMnLpnService bsMnLpnService;

	@Autowired
	private BsMtrlService bsMtrlService;

	/**
	 * 查询出库单列表
	 */
	public JqGridResponse<WmsMnForm> query(JqGridRequest<WmsMnForm> request, ParamUtils paramUtils) {
		String partnerCode = PartnerCodeHolder.getPartnerCode();
		String pointData = dataRolePointService.getSql(paramUtils.getUserCode(), "WmsMnBad.Query");
		String sql = "select w.pmn_id, w.partner_code, w.org_id, w.pmn_code, w.pmn_type, w.ref_mn_code, w.ref_mn_date, w.prod_id, w.mn_plan_qty, w.mn_tb_qty, w.mn_mp_stat, w.mn_mp_bdate, w.mn_mp_edate, w.cust_id, w.ptm_oppt, w.fl_rule, w.is_bonded, w.vm_store_id, w.mn_audit_stat, w.mn_src_type, w.rcv_type, w.rcv_stat, w.rcv_bdate, w.rcv_edate, w.omtrl_type, w.is_vm_mn, w.vm_mn_id, w.dept_id, w.sup_id, w.mn_io_type, w.inc_store_id, w.tran_cls_id, w.mn_reason, w.is_out_qc, w.ship_date, w.ship_addr, w.post_by, w.post_date, w.audit_by, w.audit_date, w.close_by, w.close_date, w.remark, w.created_by, w.created_date, w.updated_by, w.updated_date, w.wms_mn_ex1, w.wms_mn_ex2, w.wms_mn_ex3, w.wms_mn_ex4, w.wms_mn_ex5, w.wms_mn_ex6, w.wms_mn_ex7, w.wms_mn_ex8, w.wms_mn_ex9  ";
		sql += " ,c.message_key org_name,d.message_key dept_name,u1.user_code post_user_code, u2.user_code audit_user_code, u3.user_code close_user_code ";
		sql += " from WMS_MN w ";
		sql += " left join ims_company c on c.company_code=w.org_id and c.partner_code={partnerCode} ";
		sql += " left join ims_company d on d.company_code=w.dept_id and d.partner_code={partnerCode} ";
		sql += " left join ims_user u1 on u1.user_id=w.post_by ";
		sql += " left join ims_user u2 on u2.user_id=w.audit_by ";
		sql += " left join ims_user u3 on u3.user_id=w.close_by ";
		sql += " where w.pmn_type= " + WmsConstants.WmsMnPmnType.WMS_MN_PMN_TYPE_3;
		sql = CommonUtils.addCondition(sql, "w.", pointData, partnerCode, paramUtils);
		return sqlQueryUtils.queryByCondition(request, sql, WmsMnForm.class);
	}

	/**
	 * 查询单个出库单
	 * 
	 * @param WmsMnDto
	 *            查询对象
	 * @param paramUtils
	 *            参数对象
	 * @return EditDataResult<WmsMnForm>
	 */
	public EditDataResult<WmsMnForm> getById(WmsMnDto wmsMnDto, ParamUtils paramUtils) {
		// 声明对象
		Locale locale = paramUtils.getLocale();
		LogDto logDto = paramUtils.getLogDto();
		String partnerCode = PartnerCodeHolder.getPartnerCode();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		String pointData = dataRolePointService.getSql(paramUtils.getUserCode(), "WmsMnBad.Query");
		// 必填
		checkUtils.checkIsEmpty("pmnId", wmsMnDto.getPmnId(), locale);
		// 校验数据是否存在
		String sql = "select w.project_code, o.project_name, w.em_id,dept.dept_name, alias.segment, w.disposition_id,w.pmn_id, w.partner_code, w.org_id, w.pmn_code, w.pmn_type, w.ref_mn_code, w.ref_mn_date, w.prod_id, w.mn_plan_qty, w.mn_tb_qty, w.mn_mp_stat, w.mn_mp_bdate, w.mn_mp_edate, w.cust_id, w.ptm_oppt, w.fl_rule, w.is_bonded, w.vm_store_id, w.mn_audit_stat, w.mn_src_type, w.rcv_type, w.rcv_stat, w.rcv_bdate, w.rcv_edate, w.omtrl_type, w.is_vm_mn, w.vm_mn_id, w.dept_id, w.sup_id, w.mn_io_type, w.inc_store_id, w.tran_cls_id, w.mn_reason, w.is_out_qc, w.ship_date, w.ship_addr, w.post_by, w.post_date, w.audit_by, w.audit_date, w.close_by, w.close_date, w.remark, w.created_by, w.created_date, w.updated_by, w.updated_date, w.wms_mn_ex1, w.wms_mn_ex2, w.wms_mn_ex3, w.wms_mn_ex4, w.wms_mn_ex5, w.wms_mn_ex6, w.wms_mn_ex7, w.wms_mn_ex8, w.wms_mn_ex9  ";
		sql += " ,c.message_key org_name,d.message_key dept_name,u1.user_code post_user_code, u2.user_code audit_user_code, u3.user_code close_user_code ";
		sql += " from WMS_MN w ";
		sql += " left join ims_company c on c.company_code=w.org_id and c.partner_code={partnerCode} ";
		sql += " left join ims_company d on d.company_code=w.dept_id and d.partner_code={partnerCode} ";
		sql += " left join ims_user u1 on u1.user_id=w.post_by ";
		sql += " left join ims_user u2 on u2.user_id=w.audit_by ";
		sql += " left join ims_user u3 on u3.user_id=w.close_by ";
		sql += " left join bs_dept dept on dept.dept_id = w.dept_id ";
		sql += " left join ims_user emp on emp.user_id = w.em_id ";
		sql += " left join wms_oa_project o on o.project_code = w.project_code ";
		sql += " left join bs_alias alias on alias.disposition_id = w.disposition_id ";
		sql += " where w.pmn_id=? ";
		sql = CommonUtils.addCondition(sql, "w.", pointData, partnerCode, paramUtils);
		WmsMnForm wmsMnForm = sqlQueryUtils.get(sql, new Object[] { wmsMnDto.getPmnId() }, WmsMnForm.class);
		checkUtils.checkIsNotExist("mnCode", wmsMnForm, locale);
		// 检查切面权限
		checkUtils.checkHasAuthPointData(wmsMnForm.getOrgId(), dataRolePointService.getList(logDto.getUserCode(), "WmsMnBad.Query"), locale);
		// 返回
		EditDataResult<WmsMnForm> result = new EditDataResult<WmsMnForm>();
		// 查询物料需求
		sql = "select a.bs_mn_mtrl_id,a.mn_id,a.mtrl_id,a.tn_qty,a.audit_uncmp_qty,a.audit_cmp_qty,a.remark,a.updated_date," + "b.mtrl_code,b.mtrl_name,b.mtrl_desc,b.is_led,b.is_use_sku ";
		sql += ",(SELECT CASE WHEN SUM( LPN_QTY ) IS NULL THEN 0 ELSE SUM ( LPN_QTY ) END ";
		sql += " FROM bs_lpn_stock ";
		sql += " WHERE mtrl_id = a.mtrl_id AND lpn_qty > 0 AND qlty_stat = 2 AND is_rc = 'N' AND lpn_lock_type = 0 AND loc_stat=1) stock_count";
		sql += " from bs_mn_mtrl a ";
		sql += " join bs_mtrl b on a.mtrl_id=b.mtrl_id ";
		sql += " where a.mn_id=?";
		result.getExtend().put("mnMtrlList", sqlQueryUtils.queryAll(sql, new Object[] { wmsMnForm.getPmnId() }, BsMnMtrlForm.class));
		// 查询物料需求属性
		sql = "select a.bs_mn_opts_id,a.mn_id,a.bs_mn_mtrl_id,a.mtrl_id,a.lotno,a.bs_mtrl_sku_hd_id,a.sku_mn_qty,a.audit_uncmp_qty,a.audit_cmp_qty,a.remark,a.updated_date,"
				+ "b.mtrl_code,b.mtrl_name,b.mtrl_desc,b.is_led,b.is_use_sku,c.sku_code,d.sku_grp_code";
		sql += " from bs_mn_opts a ";
		sql += " join bs_mtrl b on a.mtrl_id=b.mtrl_id ";
		sql += " left join bs_mtrl_sku_hd c on c.bs_mtrl_sku_hd_id=a.bs_mtrl_sku_hd_id ";
		sql += " left join bs_sku_grp d on d.sku_grp_id=a.sku_grp_id ";
		sql += " where a.mn_id=?";
		result.getExtend().put("mnOptsList", sqlQueryUtils.queryAll(sql, new Object[] { wmsMnForm.getPmnId() }, BsMnOptsForm.class));
		// 查询出库LPN
		sql = " select a.lpn,a.lotno,a.lpn_init_qty,a.lpn_out_stat ";
		sql += " ,b.mtrl_code,b.mtrl_name,b.mtrl_desc ";
		sql += " ,c.store_code ";
		sql += " ,s.sku_code ";
		sql += " from bs_mn_lpn a ";
		sql += " inner join bs_mtrl b on a.mtrl_id=b.mtrl_id ";
		sql += " left join bs_store c on a.out_store_id=c.store_id ";
		sql += " left join bs_mtrl_sku_hd s on s.bs_mtrl_sku_hd_id=a.bs_mtrl_sku_hd_id ";
		sql += " where a.mn_id=? ";
		sql += WmsCommonUtils.getSqlByBsMnLpnBetweenOneYearsString("a");
		result.getExtend().put("mnLpnList", sqlQueryUtils.queryAll(sql, new Object[] { wmsMnForm.getPmnId() }, BsMnLpnForm.class));
		result.setCount(PangusConstants.RESULT_COUNT_ONE);
		result.setResultCode(PangusConstants.RESTULT_OK);
		result.setData(wmsMnForm);
		return result;
	}

	/**
	 * 新增出库单
	 * 
	 * @param WmsMnDto
	 *            新增对象
	 * @param paramUtils
	 *            参数对象
	 * @return EditDataResult<WmsMnDto>
	 */
	public EditDataResult<WmsMnDto> add(JsonRequest<WmsMnDto> jsonRequest, ParamUtils paramUtils) {
		// 声明对象
		WmsMnDto wmsMnDto = jsonRequest.getForm();
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		paramUtils.setCheckUtils(checkUtils);
		// 非空验证
		checkUtils.checkIsEmpty("pmnCode", wmsMnDto.getPmnCode(), locale);
		// 生成编码
		BsCodeHdFormDto bsCodeHdFormDto = new BsCodeHdFormDto();
		bsCodeHdFormDto.setCompanyCode(wmsMnDto.getOrgId());
		bsCodeHdFormDto.setCustomDate(new Date());
		bsCodeHdFormDto.setDnRuleCode(WmsConstants.dnRuleCodeName.WMS_PMN_CODE.toString());
		String pmnCode = bsCodeHdService.createdBsCodeHdCode(bsCodeHdFormDto).getData();
		wmsMnDto.setPmnCode(pmnCode);
		// 通用校验
		paramUtils.setCheckUtils(checkUtils);
		commonValidate(wmsMnDto, paramUtils, "Add");
		// 新增
		wmsMnDto.setPmnId(fastIdUtils.nextId());
		wmsMnDto.setPmnType(WmsConstants.WmsMnPmnType.WMS_MN_PMN_TYPE_3);
		// 查询领料确认范围
		String value = configService.getConfigValue(WmsConstants.SYSTEM_IMS_WMS_CODE, WmsPickingStrategy.ConfigCode.CONFIG_RCV_CONFIRM.toString(), wmsMnDto.getOrgId(), WmsPickingStrategy.ConfigCode.CONFIG_RCV_CONFIRM.getDftVal(), String.class);
		if (ElecCommonUtils.comparePhaseValue(value, WmsPickingStrategy.CONFIG_RCV_CONFIRM.WMS_OTHER_OUT_MN)) {
			wmsMnDto.setRcvType(RcvType.CONFIRM);
		} else {
			// 查询领料交易范围
			value = configService.getConfigValue(WmsConstants.SYSTEM_IMS_WMS_CODE, WmsPickingStrategy.ConfigCode.CONFIG_RCV_TRAN.toString(), wmsMnDto.getOrgId(), WmsPickingStrategy.ConfigCode.CONFIG_RCV_TRAN.getDftVal(), String.class);
			if (ElecCommonUtils.comparePhaseValue(value, WmsPickingStrategy.CONFIG_RCV_TRAN.WMS_OTHER_OUT_MN)) {
				wmsMnDto.setRcvType(RcvType.TRAN);
			} else {
				wmsMnDto.setRcvType(RcvType.NONE);
			}
		}
		wmsMnDto.setMnMpStat(WmsConstants.MnMpStat.NOT_OUT);
		wmsMnDto.setMnPlanQty(BigDecimal.ZERO);
		wmsMnDto.setMnTbQty(BigDecimal.ZERO);
		wmsMnDto.setRcvStat(RcvStat.NONE);
		wmsMnDto.setIsBonded(IsBonded.NO);
		wmsMnDto.setMnAuditStat(MN_AUDIT_STAT.MN_AUDIT_STAT_0);
		wmsMnDto.setOmtrlType(OmtrlType.DEFAULT);
		wmsMnDto.setIsVmMn(IsValid.NO);
		wmsMnDto.setMnIoType(MnIoType.SEND);
		// 查询出库品质检验范围策略
		value = configService.getConfigValue(WmsConstants.SYSTEM_IMS_WMS_CODE, ElecConstants.WmsOutStockStrategy.ConfigCode.CONFIG_OUT_QUALITY_CHECK_ALL.toString(), wmsMnDto.getOrgId(), ElecConstants.WmsOutStockStrategy.ConfigCode.CONFIG_OUT_QUALITY_CHECK_ALL.getDftVal(),
				String.class);
		if (ElecCommonUtils.comparePhaseValue(value, ElecConstants.WmsOutStockStrategy.CONFIG_OUT_QUALITY_CHECK_ALL.WMS_OTHER_OUT_MN)) {
			wmsMnDto.setIsOutQc(IsOutQc.YES);
		} else {
			wmsMnDto.setIsOutQc(IsOutQc.NOT);
		}
		wmsMnDto.setCreatedBy(paramUtils.getUserId());
		wmsMnDto.setUpdatedBy(paramUtils.getUserId());
		wmsMnDto.setCreatedDate(SysContextHolder.get().getCurrentDate());
		wmsMnDto.setUpdatedDate(SysContextHolder.get().getCurrentDate());
		int count = this.insertNotNull(wmsMnDto);
		// 校验返回值
		checkUtils.checkInsertDataIsOk(count, 1, (JSONObject) JSONObject.toJSON(wmsMnDto), WmsMnDto.class);
		// 保存子表数据
		this.saveChildTabData(jsonRequest, paramUtils);
		// 返回
		EditDataResult<WmsMnDto> result = new EditDataResult<WmsMnDto>();
		result.setData(wmsMnDto);
		result.setCount(count);
		result.setResultMsg(MsgUtils.saveMsg(null, messageSource, locale));
		return result;
	}

	/**
	 * 修改出库单
	 * 
	 * @param WmsMnDto
	 *            新增对象
	 * @param paramUtils
	 *            参数对象
	 * @return EditDataResult<WmsMnDto>
	 */
	public EditDataResult<WmsMnDto> edit(JsonRequest<WmsMnDto> jsonRequest, ParamUtils paramUtils) {
		// 声明对象
		WmsMnDto wmsMnDto = jsonRequest.getForm();
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		paramUtils.setCheckUtils(checkUtils);
		// 通用校验
		paramUtils.setCheckUtils(checkUtils);
		commonValidate(wmsMnDto, paramUtils, "Edit");
		// 验证数据是否不存在
		WmsMnDto wmsMnDb = this.selectByPrimaryKey(wmsMnDto);
		checkUtils.checkIsNotExist("mnCode", wmsMnDb, locale);
		// 只有制单状态才能修改
		checkUtils.checkIsWrongState("error.codeisnotmadenoupdate", wmsMnDb.getMnAuditStat(), MN_AUDIT_STAT.MN_AUDIT_STAT_0, locale);
		// 验证不能修改的数据是否被修改：编码不可修改
		checkUtils.checkIsChanged(new String[] { "partnerCode", "orgId", "pmnCode" }, (JSONObject) JSONObject.toJSON(wmsMnDto), (JSONObject) JSONObject.toJSON(wmsMnDb), locale);
		// 验证数据是否已被修改
		checkUtils.checkIsChanged(wmsMnDto.getUpdatedDate(), wmsMnDb.getUpdatedDate(), locale);
		// 修改
		wmsMnDto.setUpdatedBy(paramUtils.getUserId());
		wmsMnDto.setUpdatedDate(SysContextHolder.get().getCurrentDate());
		Condition condition = new Condition(WmsMnDto.class);
		Criteria criteria = condition.createCriteria();
		criteria.andEqualTo("pmnId", wmsMnDto.getPmnId());
		criteria.andEqualTo("mnAuditStat", MN_AUDIT_STAT.MN_AUDIT_STAT_0);
		int count = this.updateByConditionNotNull(wmsMnDto, condition);
		// 校验返回值
		checkUtils.checkUpdateDataIsOk(count, 1, (JSONObject) JSONObject.toJSON(wmsMnDto), WmsMnDto.class);
		// 保存子表数据
		this.saveChildTabData(jsonRequest, paramUtils);
		// 封装返回数据
		EditDataResult<WmsMnDto> result = new EditDataResult<WmsMnDto>();
		result.setCount(count);
		result.setData(wmsMnDto);
		result.setResultMsg(MsgUtils.saveMsg(null, messageSource, locale));
		return result;
	}

	/**
	 * 删除出库单
	 * 
	 * @param dtoList
	 *            删除对象
	 * @param paramUtils
	 *            参数对象
	 * @return EditDataResult<WmsMnDto>
	 */
	public EditDataResult<WmsMnDto> delete(List<WmsMnDto> dtoList, ParamUtils paramUtils) {
		// 声明对象
		int count = 0;
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		// 校验空值
		checkUtils.checkIsEmpty("pmnId", dtoList, locale);
		for (WmsMnDto dto : dtoList) {
			// 非空验证
			checkUtils.checkIsEmpty("pmnId", dto.getPmnId(), locale);
			checkUtils.checkIsEmpty("updatedDate", dto.getUpdatedDate(), locale);
			// 验证数据是否不存在
			WmsMnDto wmsMnDb = this.selectByPrimaryKey(dto);
			checkUtils.checkIsNotExist("pmnCode", wmsMnDb, locale);
			// 只有单据状态为制单中才能删除
			checkUtils.checkIsWrongState("error.codeisnotmadenodelete", wmsMnDb.getMnAuditStat(), MN_AUDIT_STAT.MN_AUDIT_STAT_0, locale);
			// 校验是否有此数据权限
			List<String> pointData = dataRolePointService.getList(logDto.getUserCode(), "WmsMnBad.Delete");
			companyService.checkCompanyData(wmsMnDb.getOrgId(), pointData, checkUtils, locale);
			// 删除
			Condition condition = new Condition(WmsMnDto.class);
			Criteria criteria = condition.createCriteria();
			criteria.andEqualTo("pmnId", dto.getPmnId());
			criteria.andEqualTo("mnAuditStat", MN_AUDIT_STAT.MN_AUDIT_STAT_0);
			int deleteCount = this.deleteByCondition(condition);
			// 校验返回值
			checkUtils.checkDeleteDataIsOk(deleteCount, 1, (JSONObject) JSONObject.toJSON(dto), WmsMnDto.class);
			count = count + deleteCount;
			// 删除物料需求
			BsMnMtrlDto bsMnMtrlDto = new BsMnMtrlDto();
			bsMnMtrlDto.setMnId(dto.getPmnId());
			bsMnMtrlService.delete(bsMnMtrlDto);
			// 删除物料属性需求
			BsMnOptsDto bsMnOptsDto = new BsMnOptsDto();
			bsMnOptsDto.setMnId(dto.getPmnId());
			bsMnOptsService.delete(bsMnOptsDto);
		}
		// 返回
		EditDataResult<WmsMnDto> result = new EditDataResult<WmsMnDto>();
		result.setCount(count);
		result.setResultCode(PangusConstants.RESTULT_OK);
		result.setResultMsg(MsgUtils.delMsg(null, messageSource, locale));
		return result;
	}

	/**
	 * 检查工厂权限
	 * 
	 * @param dtoList
	 *            检查对象
	 * @param dataPointId
	 *            切入点Id
	 * @param paramUtils
	 *            参数对象
	 * @return EditDataResult<List<String>>
	 */
	public EditDataResult<List<String>> checkPointData(List<WmsMnDto> dtoList, String dataPointId, ParamUtils paramUtils) {
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		EditDataResult<List<String>> result = new EditDataResult<List<String>>();
		CheckUtils checkUtil = new CheckUtils(messageSource, parameterService, logDto);
		// 验证非空
		checkUtil.checkIsEmpty(PangusConstants.DATA_POINT_ID, dataPointId, locale);
		// 验证是否有工厂权限
		List<String> pointData = dataRolePointService.getList(paramUtils.getUserCode(), dataPointId.toString());
		for (WmsMnDto dto : dtoList) {
			WmsMnDto dbData = this.selectByPrimaryKey(dto);
			checkUtil.checkIsNotExist("pmnId", dbData, locale);
			String companyCode = dbData.getOrgId();
			// 校验是否有此数据权限
			checkUtil.checkHasAuthPointData(companyCode, pointData, locale);
		}
		// 返回
		result.setCount(1);
		result.setData(pointData);
		return result;
	}

	/**
	 * 通用检验
	 * 
	 * @param WmsMnDto
	 *            验证对象
	 * @param paramUtils
	 *            参数对象
	 */
	public void commonValidate(WmsMnDto wmsMnDto, ParamUtils paramUtils, String type) {
		// 声明对象
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = paramUtils.getCheckUtils();
		// 非空验证
		checkUtils.checkIsEmpty("pmnId", wmsMnDto, locale);
		checkUtils.checkIsEmpty("pmnCode", wmsMnDto.getPmnCode(), locale);
		if (type == "Edit") {
			checkUtils.checkIsEmpty("pmnId", wmsMnDto.getPmnId(), locale);
			checkUtils.checkIsEmpty("updatedDate", wmsMnDto.getUpdatedDate(), locale);
		}
		checkUtils.checkIsEmpty("orgId", wmsMnDto.getOrgId(), locale);
		// 验证下拉框参数范围值
		checkUtils.checkIsInType(new String[] { "ptmOppt", "flRule", "mnSrcType" }, (JSONObject) JSONObject.toJSON(wmsMnDto), locale);
		// 验证关联数据值，工厂编码存在性及是否有权限
		List<String> pointData = dataRolePointService.getList(logDto.getUserCode(), "WmsMnBad." + type);
		companyService.checkCompanyData(wmsMnDto.getOrgId(), pointData, checkUtils, locale);
		// 验证唯一索引
		Condition condition = new Condition(WmsMnDto.class);
		Criteria criteria = condition.createCriteria();
		criteria.andNotEqualTo("pmnId", wmsMnDto.getPmnId());
		criteria.andEqualTo("pmnCode", wmsMnDto.getPmnCode());
		criteria.andEqualTo("orgId", wmsMnDto.getOrgId());
		criteria.andEqualTo("partnerCode", PartnerCodeHolder.getPartnerCode());
		List<WmsMnDto> wmsMnDtos = this.selectByCondition(condition);
		checkUtils.checkIsExist("pmnCode", wmsMnDtos, locale);
	}

	/**
	 * 校验物料，组合物料冲突 ，组合物料子物料比例，物料需求属性对应的物料总数量是否和物料数量相等
	 * 
	 * @param request
	 * @return
	 */
	public void checkMtrl(List<BsMnMtrlDto> bsMnMtrlDtos, List<BsMnOptsDto> bsMnOptsDtos, ParamUtils paramUtils) {
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = paramUtils.getCheckUtils();
		if (bsMnMtrlDtos == null || bsMnMtrlDtos.size() == 0) {
			// 物料需求不能为空
			String mnMtrlStr = messageSource.getMessage("table.bsmnmtrl", locale);
			checkUtils.throwException(messageSource.getMessage("common.notempty", new Object[] { mnMtrlStr }, locale), new ValueEmptyException());
		}
		List<Object> mtrlIdList = new ArrayList<>();
		Map<String, BigDecimal> mtrlQty = new HashMap<>(); // 物料数量{mtrlId:0,mtrlId$bsMnMtrlId:0}
		Map<String, BigDecimal> optsQty = new HashMap<>(); // 物料属性数量统计
		for (BsMnMtrlDto dto : bsMnMtrlDtos) {
			String mtrlId = dto.getMtrlId();
			mtrlIdList.add(mtrlId);
			mtrlQty.put(mtrlId + "$" + dto.getBsMnMtrlId(), dto.getTnQty()); // 按（物料编码$物料需求代码）统计，用于校验物料属性数量
			BigDecimal qty = mtrlQty.getOrDefault(mtrlId, BigDecimal.ZERO);
			mtrlQty.put(mtrlId, qty.add(dto.getTnQty())); // 按（物料编码）统计，用于校验组合物料比列
		}

		// 校验物料属性数量是否和物料数量相等
		// 根据物料编码和物料需求代码统计物料属性数量
		for (BsMnOptsDto dto : bsMnOptsDtos) {
			String mtrlId = dto.getMtrlId();
			String key = mtrlId + "$" + dto.getBsMnMtrlId();
			BigDecimal qty = optsQty.getOrDefault(key, BigDecimal.ZERO);
			optsQty.put(key, qty.add(dto.getSkuMnQty()));
		}
		for (Map.Entry<String, BigDecimal> entry : optsQty.entrySet()) {
			String key = entry.getKey();
			BigDecimal qty = entry.getValue();
			if (qty.compareTo(mtrlQty.get(key)) != 0) {
				// 物料编码：{0}数量{1}与属性数量{2}不相等，不能操作
				BsMtrlDto bsMtrlDto = bsMtrlService.selectByPrimaryKeyNotNull(new BsMtrlDto(key.split("\\$")[0]));
				checkUtils.throwException(messageSource.getMessage("error.mtrlqtynotequalsoptsqty", new Object[] { bsMtrlDto.getMtrlCode(), mtrlQty.get(key), qty }, locale), new BaseException());
			}
		}
	}

	@Override
	public void importConvert(int i, List<WmsMnForm> list, ParamUtils paramUtils) {
		for (WmsMnForm WmsMnForm : list) {
			commonValidate(WmsMnForm, paramUtils, "Add");
		}
	}

	@Override
	public void exportConvert(int j, List<WmsMnForm> list, ParamUtils paramUtils) {
	}

	/**
	 * 根据组织获取出库策略
	 * 
	 * @return
	 */
	public EditDataResult<WmsMnDto> getOutStockStrategy(WmsMnDto wmsMnDto, ParamUtils paramUtils) {
		EditDataResult<WmsMnDto> result = new EditDataResult<>();
		String orgId = wmsMnDto.getOrgId();
		// 获取出库策略中虚拟库范围
		String configVmCode = ElecConstants.WmsOutStockStrategy.CONFIG_VM_RANGE.ConfigCode.CONFIG_VM_CODE.toString();
		String configType = ElecConstants.WmsOutStockStrategy.CONFIG_VM_RANGE.ConfigType.WMS_PRODUCT_OUT_MN.toString();
		String configVmValue = configService.getConfigValue(WmsConstants.SYSTEM_IMS_WMS_CODE, configVmCode, configType, orgId, "", String.class);
		if (StringUtils.isBlank(configVmValue)) {
			result.getExtend().put("vmRange", "");
		} else {
			result.getExtend().put("vmRange", configVmValue);
			result.getExtend().put("storeCode", configVmValue);
		}
		String value = "";
		// 虚拟库范围中其他出库单的虚拟库编码策略
		ConfigDto configDto = new ConfigDto();
		configDto.setCompanyCode(wmsMnDto.getOrgId());
		configDto.setConfigCode(WmsConstants.VmRangeStrategy.ConfigCode.CONFIG_VM_CODE.toString());
		configDto.setConfigType(WmsConstants.VmRangeStrategy.ConfigTypeCode.WMS_OTHER_OUT_MN.toString());
		configDto.setSystemCode(WmsConstants.SYSTEM_IMS_WMS_CODE);
		configDto = configService.selectFirst(configDto);
		if (StringUtils.isNotEmpty(configDto) && StringUtils.isNotEmpty(configDto.getConfigValue())) {
			String vmStoreId = configDto.getConfigValue();
			BsStoreDto bsStoreDto = bsStoreService.selectByPrimaryKeyNotNull(new BsStoreDto(vmStoreId));
			result.getExtend().put("vmStoreCode", bsStoreDto.getStoreCode());
		}
		// 获取出库策略的出库规则
		value = configService.getConfigValue(WmsConstants.SYSTEM_IMS_WMS_CODE, WmsOutStockStrategy.ConfigCode.CONFIG_OUT_RANGE.toString(), orgId, WmsOutStockStrategy.ConfigCode.CONFIG_OUT_RANGE.getDftVal(), String.class);
		result.getExtend().put("outRange", value);
		return result;
	}

	/**
	 * 保存子表数据
	 * 
	 * @param jsonRequest
	 * @param paramUtils
	 */
	private void saveChildTabData(JsonRequest<WmsMnDto> jsonRequest, ParamUtils paramUtils) {
		WmsMnDto wmsMnDto = jsonRequest.getForm();
		List<BsMnMtrlDto> mnMtrlList = JSONObject.parseArray(jsonRequest.getExtend().get("mnMtrlList"), BsMnMtrlDto.class);
		List<BsMnOptsDto> mnOptsList = JSONObject.parseArray(jsonRequest.getExtend().get("mnOptsList"), BsMnOptsDto.class);
		this.checkMtrl(mnMtrlList, mnOptsList, paramUtils);
		Map<String, BigDecimal> map = new HashMap<>();
		for (BsMnMtrlDto bsMnMtrlDto : mnMtrlList) {
			BigDecimal sum = map.getOrDefault(bsMnMtrlDto.getMtrlId(), BigDecimal.ZERO);
			sum.add(bsMnMtrlDto.getTnQty());
			map.put(bsMnMtrlDto.getMtrlId(), sum);
		}
		for (BsMnMtrlDto bsMnMtrlDto : mnMtrlList) {
			if (bsMnMtrlService.selectByPrimaryKey(bsMnMtrlDto) == null) {
				// 新增
				bsMnMtrlDto.setMnId(wmsMnDto.getPmnId());
				bsMnMtrlDto.setNnQty(bsMnMtrlDto.getTnQty());
				bsMnMtrlDto.setIsMain(IS_MAIN.Y);
				bsMnMtrlDto.setMainId(bsMnMtrlDto.getMtrlId());
				WmsBsMtrlDto wmsBsMtrlDto = wmsBsMtrlService.selectByPrimaryKey(new WmsBsMtrlDto(bsMnMtrlDto.getMtrlId()));
				if (wmsBsMtrlDto != null && wmsBsMtrlDto.getFlDay() != null && wmsBsMtrlDto.getFlDay().compareTo(BigDecimal.ZERO) > 0) {
					bsMnMtrlDto.setFlDay(wmsBsMtrlDto.getFlDay());
				} else {
					BsMtrlDto bsMtrlDto = bsMtrlService.selectByPrimaryKeyNotNull(new BsMtrlDto(bsMnMtrlDto.getMtrlId()));
					if (MtrlLev.A == bsMtrlDto.getMtrlLev()) {
						// A材出库粒度
						Integer value = configService.getConfigValue(WmsConstants.SYSTEM_IMS_WMS_CODE, WmsOutStockStrategy.ConfigCode.CONFIG_A_OUT_RANGE.toString(), wmsMnDto.getOrgId(), WmsOutStockStrategy.ConfigCode.CONFIG_A_OUT_RANGE.getDftVal(), Integer.class);
						bsMnMtrlDto.setFlDay(new BigDecimal(value));
					} else if (MtrlLev.B == bsMtrlDto.getMtrlLev()) {
						// B材出库粒度
						Integer value = configService.getConfigValue(WmsConstants.SYSTEM_IMS_WMS_CODE, WmsOutStockStrategy.ConfigCode.CONFIG_A_OUT_RANGE.toString(), wmsMnDto.getOrgId(), WmsOutStockStrategy.ConfigCode.CONFIG_A_OUT_RANGE.getDftVal(), Integer.class);
						bsMnMtrlDto.setFlDay(new BigDecimal(value));
					} else if (MtrlLev.C == bsMtrlDto.getMtrlLev()) {
						// C材出库粒度
						Integer value = configService.getConfigValue(WmsConstants.SYSTEM_IMS_WMS_CODE, WmsOutStockStrategy.ConfigCode.CONFIG_A_OUT_RANGE.toString(), wmsMnDto.getOrgId(), WmsOutStockStrategy.ConfigCode.CONFIG_A_OUT_RANGE.getDftVal(), Integer.class);
						bsMnMtrlDto.setFlDay(new BigDecimal(value));
					}
					if (bsMnMtrlDto.getFlDay() == null || bsMnMtrlDto.getFlDay().compareTo(BigDecimal.ZERO) == 0) {
						bsMnMtrlDto.setFlDay(BigDecimal.ONE);
					}
				}
				bsMnMtrlDto.setTnQtyS(map.getOrDefault(bsMnMtrlDto.getMtrlId(), bsMnMtrlDto.getTnQty()));
				bsMnMtrlDto.setNnQtyS(map.getOrDefault(bsMnMtrlDto.getMtrlId(), bsMnMtrlDto.getNnQty()));
				bsMnMtrlDto.setDosage(BigDecimal.ZERO);
				bsMnMtrlDto.setNotMerge(NOT_MERGE.N);
				bsMnMtrlDto.setMnSrcType(WmsConstants.MnSrcType.MNSRC_TYPE_1);
				String bsMnMtrlId = bsMnMtrlDto.getBsMnMtrlId();
				bsMnMtrlService.add(bsMnMtrlDto, paramUtils);
				for (BsMnOptsDto bsMnOptsDto : mnOptsList) {
					if (bsMnMtrlId.equals(bsMnOptsDto.getBsMnMtrlId())) {
						bsMnOptsDto.setBsMnMtrlId(bsMnMtrlDto.getBsMnMtrlId());
					}
				}
			} else {
				// 更新
				bsMnMtrlDto.setMainId(bsMnMtrlDto.getMtrlId());
				bsMnMtrlDto.setNnQty(bsMnMtrlDto.getTnQty());
				bsMnMtrlDto.setTnQtyS(map.getOrDefault(bsMnMtrlDto.getMtrlId(), bsMnMtrlDto.getTnQty()));
				bsMnMtrlDto.setNnQtyS(map.getOrDefault(bsMnMtrlDto.getMtrlId(), bsMnMtrlDto.getNnQty()));
				bsMnMtrlService.edit(bsMnMtrlDto, paramUtils);
			}
		}
		for (BsMnOptsDto bsMnOptsDto : mnOptsList) {
			if (bsMnOptsService.selectByPrimaryKey(bsMnOptsDto) == null) {
				// 新增
				bsMnOptsDto.setMnId(wmsMnDto.getPmnId());
				bsMnOptsService.add(bsMnOptsDto, paramUtils);
			} else {
				// 更新
				bsMnOptsService.edit(bsMnOptsDto, paramUtils);
			}
		}
	}

	/**
	 * 提交
	 * 
	 * @param dtoList
	 * @param paramUtils
	 * @return
	 */
	public EditDataResult<WmsMnDto> submit(List<WmsMnDto> dtoList, ParamUtils paramUtils) {
		// 声明对象
		int count = 0;
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		paramUtils.setCheckUtils(checkUtils);
		// 校验空值
		checkUtils.checkIsEmpty("pmnId", dtoList, locale);
		for (WmsMnDto dto : dtoList) {
			// 非空验证
			checkUtils.checkIsEmpty("pmnId", dto.getPmnId(), locale);
			checkUtils.checkIsEmpty("updatedDate", dto.getUpdatedDate(), locale);
			// 验证数据是否不存在
			WmsMnDto wmsMnDb = this.selectByPrimaryKey(dto);
			checkUtils.checkIsNotExist("pmnCode", wmsMnDb, locale);
			// 校验是否有此数据权限
			List<String> pointData = dataRolePointService.getList(logDto.getUserCode(), "WmsMnBad.Submit");
			companyService.checkCompanyData(wmsMnDb.getOrgId(), pointData, checkUtils, locale);
			// 只有单据状态为制单中才能提交
			checkUtils.checkIsWrongState("error.codeisnotmadenosubmit", wmsMnDb.getMnAuditStat(), MN_AUDIT_STAT.MN_AUDIT_STAT_0, locale);
			// 验证数据是否已被修改
			checkUtils.checkIsChanged(dto.getUpdatedDate(), wmsMnDb.getUpdatedDate(), locale);
			// 更新
			WmsMnDto wmsMnDto = new WmsMnDto();
			wmsMnDto.setPmnId(dto.getPmnId());
			wmsMnDto.setMnAuditStat(MN_AUDIT_STAT.MN_AUDIT_STAT_1);
			wmsMnDto.setUpdatedBy(paramUtils.getUserId());
			wmsMnDto.setUpdatedDate(SysContextHolder.get().getCurrentDate());
			wmsMnDto.setPostBy(paramUtils.getUserId());
			wmsMnDto.setPostDate(SysContextHolder.get().getCurrentDate());
			Condition condition = new Condition(WmsMnDto.class);
			Criteria criteria = condition.createCriteria();
			criteria.andEqualTo("pmnId", dto.getPmnId());
			criteria.andEqualTo("mnAuditStat", MN_AUDIT_STAT.MN_AUDIT_STAT_0);
			int updateCount = this.updateByConditionNotNull(wmsMnDto, condition);
			// 验证返回值
			checkUtils.checkUpdateDataIsOk(updateCount, 1, (JSONObject) JSONObject.toJSON(dto), WmsMnDto.class);
			count = count + updateCount;
		}
		// 返回
		EditDataResult<WmsMnDto> result = new EditDataResult<WmsMnDto>();
		result.setCount(count);
		result.setResultCode(PangusConstants.RESTULT_OK);
		result.setResultMsg(MsgUtils.delMsg(null, messageSource, locale));
		return result;
	}

	/**
	 * 退单
	 * 
	 * @param dtoList
	 * @param paramUtils
	 * @return
	 */
	public EditDataResult<WmsMnDto> refund(List<WmsMnDto> dtoList, ParamUtils paramUtils) {
		// 声明对象
		int count = 0;
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		// 校验空值
		checkUtils.checkIsEmpty("pmnId", dtoList, locale);
		for (WmsMnDto dto : dtoList) {
			// 非空验证
			checkUtils.checkIsEmpty("pmnId", dto.getPmnId(), locale);
			checkUtils.checkIsEmpty("updatedDate", dto.getUpdatedDate(), locale);
			// 验证数据是否不存在
			WmsMnDto wmsmnDb = this.selectByPrimaryKey(dto);
			checkUtils.checkIsNotExist("pmnCode", wmsmnDb, locale);
			// 只有单据状态为已提交才能退单
			checkUtils.checkIsWrongState("error.codeisnotsubmitnorefund", wmsmnDb.getMnAuditStat(), MN_AUDIT_STAT.MN_AUDIT_STAT_1, locale);
			// 校验是否有此数据权限
			List<String> pointData = dataRolePointService.getList(logDto.getUserCode(), "WmsMnBad.Refund");
			companyService.checkCompanyData(wmsmnDb.getOrgId(), pointData, checkUtils, locale);
			// 验证数据是否已被修改
			checkUtils.checkIsChanged(dto.getUpdatedDate(), wmsmnDb.getUpdatedDate(), locale);
			// 更新
			Map<String, Object> cmap = new HashMap<String, Object>();
			cmap.put("pmnId", dto.getPmnId());
			cmap.put("mnAuditStat", MN_AUDIT_STAT.MN_AUDIT_STAT_1);
			Condition condition = checkUtils.getConditionByMap(new Condition(WmsMnDto.class), cmap);
			wmsmnDb.setMnAuditStat(MN_AUDIT_STAT.MN_AUDIT_STAT_0);
			wmsmnDb.setUpdatedBy(paramUtils.getUserId());
			wmsmnDb.setUpdatedDate(SysContextHolder.get().getCurrentDate());
			wmsmnDb.setPostBy(null);
			wmsmnDb.setPostDate(null);
			int updateCount = this.updateByCondition(wmsmnDb, condition);
			checkUtils.checkUpdateDataIsOk(updateCount, 1, cmap, WmsMnDto.class);
			count = count + updateCount;
		}
		// 返回
		EditDataResult<WmsMnDto> result = new EditDataResult<WmsMnDto>();
		result.setCount(count);
		result.setResultCode(PangusConstants.RESTULT_OK);
		result.setResultMsg(MsgUtils.delMsg(null, messageSource, locale));
		return result;
	}

	/**
	 * 审核
	 * 
	 * @param dtoList
	 * @param paramUtils
	 * @return
	 */
	public EditDataResult<WmsMnDto> audit(List<WmsMnDto> dtoList, ParamUtils paramUtils) {
		// 声明对象
		int count = 0;
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		// 校验空值
		checkUtils.checkIsEmpty("pmnId", dtoList, locale);
		for (WmsMnDto dto : dtoList) {
			// 非空验证
			checkUtils.checkIsEmpty("pmnId", dto.getPmnId(), locale);
			checkUtils.checkIsEmpty("updatedDate", dto.getUpdatedDate(), locale);
			// 验证数据是否不存在
			WmsMnDto wmsMnDb = this.selectByPrimaryKey(dto);
			checkUtils.checkIsNotExist("pmnCode", wmsMnDb, locale);
			// 只有单据状态为已提交才能审核
			checkUtils.checkIsWrongState("error.codeisnotsubmitnoaudit", wmsMnDb.getMnAuditStat(), MN_AUDIT_STAT.MN_AUDIT_STAT_1, locale);
			// 校验是否有此数据权限
			List<String> pointData = dataRolePointService.getList(logDto.getUserCode(), "WmsMnBad.Audit");
			companyService.checkCompanyData(wmsMnDb.getOrgId(), pointData, checkUtils, locale);
			// 验证数据是否已被修改
			checkUtils.checkIsChanged(dto.getUpdatedDate(), wmsMnDb.getUpdatedDate(), locale);
			// 更新
			WmsMnDto wmsMnDto = new WmsMnDto();
			wmsMnDto.setPmnId(dto.getPmnId());
			wmsMnDto.setMnAuditStat(MN_AUDIT_STAT.MN_AUDIT_STAT_2);
			wmsMnDto.setUpdatedBy(paramUtils.getUserId());
			wmsMnDto.setUpdatedDate(SysContextHolder.get().getCurrentDate());
			wmsMnDto.setAuditBy(paramUtils.getUserId());
			wmsMnDto.setAuditDate(SysContextHolder.get().getCurrentDate());
			Condition condition = new Condition(WmsMnDto.class);
			Criteria criteria = condition.createCriteria();
			criteria.andEqualTo("pmnId", dto.getPmnId());
			criteria.andEqualTo("mnAuditStat", MN_AUDIT_STAT.MN_AUDIT_STAT_1);
			int updateCount = this.updateByConditionNotNull(wmsMnDto, condition);
			// 验证返回值
			checkUtils.checkUpdateDataIsOk(updateCount, 1, (JSONObject) JSONObject.toJSON(dto), WmsMnDto.class);
			count = count + updateCount;
		}
		// 返回
		EditDataResult<WmsMnDto> result = new EditDataResult<WmsMnDto>();
		result.setCount(count);
		result.setResultCode(PangusConstants.RESTULT_OK);
		result.setResultMsg(MsgUtils.delMsg(null, messageSource, locale));
		return result;
	}

	/**
	 * 反审核
	 * 
	 * @param dtoList
	 * @param paramUtils
	 * @return
	 */
	public EditDataResult<WmsMnDto> unaudit(List<WmsMnDto> dtoList, ParamUtils paramUtils) {
		// 声明对象
		int count = 0;
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		// 校验空值
		checkUtils.checkIsEmpty("pmnId", dtoList, locale);
		for (WmsMnDto dto : dtoList) {
			// 非空验证
			checkUtils.checkIsEmpty("pmnId", dto.getPmnId(), locale);
			checkUtils.checkIsEmpty("updatedDate", dto.getUpdatedDate(), locale);
			// 验证数据是否不存在
			WmsMnDto wmsMnDb = this.selectByPrimaryKey(dto);
			checkUtils.checkIsNotExist("pmnCode", wmsMnDb, locale);
			// 只有单据状态为已审核才能反审核
			checkUtils.checkIsWrongState("error.codeisnotauditnounaudit", wmsMnDb.getMnAuditStat(), MN_AUDIT_STAT.MN_AUDIT_STAT_2, locale);
			// 判断是否已经扫描了LPN
			BsMnLpnDto bsMnLpnDto = new BsMnLpnDto();
			bsMnLpnDto.setMnId(dto.getPmnId());
			if (bsMnLpnService.count(bsMnLpnDto) > 0) {
				checkUtils.throwException(messageSource.getMessage("error.pmncodealreadyoutcannotunaudit", new Object[] { wmsMnDb.getPmnCode() }, locale), new BaseException());
			}
			// 校验是否有此数据权限
			List<String> pointData = dataRolePointService.getList(logDto.getUserCode(), "WmsMnBad.Unaudit");
			companyService.checkCompanyData(wmsMnDb.getOrgId(), pointData, checkUtils, locale);
			// 验证数据是否已被修改
			checkUtils.checkIsChanged(dto.getUpdatedDate(), wmsMnDb.getUpdatedDate(), locale);
			// 更新
			Map<String, Object> cmap = new HashMap<String, Object>();
			cmap.put("pmnId", dto.getPmnId());
			cmap.put("mnAuditStat", MN_AUDIT_STAT.MN_AUDIT_STAT_2);
			Condition condition = checkUtils.getConditionByMap(new Condition(WmsMnDto.class), cmap);
			wmsMnDb.setMnAuditStat(MN_AUDIT_STAT.MN_AUDIT_STAT_1);
			wmsMnDb.setUpdatedBy(paramUtils.getUserId());
			wmsMnDb.setUpdatedDate(SysContextHolder.get().getCurrentDate());
			wmsMnDb.setAuditBy(null);
			wmsMnDb.setAuditDate(null);
			int updateCount = this.updateByCondition(wmsMnDb, condition);
			checkUtils.checkUpdateDataIsOk(updateCount, 1, cmap, WmsMnDto.class);
			count = count + updateCount;
		}
		// 返回
		EditDataResult<WmsMnDto> result = new EditDataResult<>();
		result.setCount(count);
		result.setResultCode(PangusConstants.RESTULT_OK);
		result.setResultMsg(MsgUtils.delMsg(null, messageSource, locale));
		return result;
	}

	/**
	 * 关单
	 * 
	 * @param dtoList
	 * @param paramUtils
	 * @return
	 */
	public EditDataResult<WmsMnDto> saveByCloseorder(List<WmsMnDto> dtoList, ParamUtils paramUtils) {
		// 声明对象
		int count = 0;
		LogDto logDto = paramUtils.getLogDto();
		Locale locale = paramUtils.getLocale();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		// 校验空值
		checkUtils.checkIsEmpty("pmnId", dtoList, locale);
		for (WmsMnDto dto : dtoList) {
			// 非空验证
			checkUtils.checkIsEmpty("pmnId", dto.getPmnId(), locale);
			checkUtils.checkIsEmpty("updatedDate", dto.getUpdatedDate(), locale);
			// 验证数据是否不存在
			WmsMnDto wmsMnDb = this.selectByPrimaryKey(dto);
			checkUtils.checkIsNotExist("pmnCode", wmsMnDb, locale);
			// 只有单据状态为已审核才能关单
			checkUtils.checkIsWrongState("error.codeisnotclosenoaudit", wmsMnDb.getMnAuditStat(), MN_AUDIT_STAT.MN_AUDIT_STAT_2, locale);
			// 校验是否有此数据权限
			List<String> pointData = dataRolePointService.getList(logDto.getUserCode(), "WmsMnBad.Closeorder");
			companyService.checkCompanyData(wmsMnDb.getOrgId(), pointData, checkUtils, locale);
			// 验证数据是否已被修改
			checkUtils.checkIsChanged(dto.getUpdatedDate(), wmsMnDb.getUpdatedDate(), locale);
			// 更新
			Map<String, Object> cmap = new HashMap<String, Object>();
			cmap.put("pmnId", dto.getPmnId());
			cmap.put("mnAuditStat", MN_AUDIT_STAT.MN_AUDIT_STAT_2);
			Condition condition = checkUtils.getConditionByMap(new Condition(WmsMnDto.class), cmap);
			WmsMnDto wmsMnDto = new WmsMnDto();
			wmsMnDto.setMnAuditStat(MN_AUDIT_STAT.MN_AUDIT_STAT_3);
			wmsMnDto.setUpdatedBy(paramUtils.getUserId());
			wmsMnDto.setUpdatedDate(SysContextHolder.get().getCurrentDate());
			wmsMnDto.setCloseBy(paramUtils.getUserId());
			wmsMnDto.setCloseDate(SysContextHolder.get().getCurrentDate());
			int updateCount = this.updateByConditionNotNull(wmsMnDto, condition);
			checkUtils.checkUpdateDataIsOk(updateCount, 1, cmap, WmsMnDto.class);
			count = count + updateCount;
		}
		// 返回
		EditDataResult<WmsMnDto> result = new EditDataResult<>();
		result.setCount(count);
		result.setResultCode(PangusConstants.RESTULT_OK);
		result.setResultMsg(MsgUtils.delMsg(null, messageSource, locale));
		return result;
	}

	@Override
	public List<String> importByExcel(int i, List<WmsMnForm> list, ParamUtils paramUtils) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <BsMnMtrlForm> EditResult verifyData(Map<String, Object> rowData, int i, ParamUtils paramUtils, JsonRequest<BsMnMtrlForm> extend) {

		List ext = JSON.parseObject(extend.getExtend().get("params"), List.class);
		// LogDto logDto = paramUtils.getLogDto();
		// CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		Locale locale = paramUtils.getLocale();
		List<String> errMsgArray = new ArrayList<String>();// 保存异常
		EditResult result = new EditResult();
		String partnerCode = PartnerCodeHolder.getPartnerCode();
		String orgId = (String) ext.get(0);
		Integer mtrlPtype = Integer.parseInt((String) ext.get(1));
		String mtrlId = "";

		// 根据物料编码查询物料是否存在
		if (!StringUtils.isEmpty((String) rowData.get("mtrlCode"))) {
			String mtrlCode = (String) rowData.get("mtrlCode");
			Condition condition = new Condition(BsMtrlDto.class);
			Criteria criteria = condition.createCriteria();
			criteria.andEqualTo("mtrlCode", mtrlCode);
			criteria.andEqualTo("orgId", orgId);
			criteria.andEqualTo("isValid", "Y");
			List<BsMtrlDto> mtrlList = bsMtrlService.selectByCondition(condition);
			if (null == mtrlList || mtrlList.size() == 0) {
				String fieldName = messageSource.getMessage("column.mtrlcode", locale);
				errMsgArray.add(messageSource.getMessage("common.checkimport.hadnotexisted", new Object[]{i+1,fieldName}, locale));
			} else {
				mtrlId = mtrlList.get(0).getMtrlId();
				String sql = "SELECT b.mtrl_id, b.partner_code, b.org_id, b.mtrl_code, b.mtrl_name, b.mtrl_desc, b.erp_mtrl_id, b.mtype_id, b.mtrl_spcl, b.pack_qty,"
						+ " b.min_sk_qty, b.max_sk_qty, b.mtrl_lev, b.mg_type, b.is_use_sku, b.munit, b.pkg_cap, b.is_msd, b.is_led, b.sl_id, b.is_valid, b.remark,"
						+ " b.created_by, b.created_date, b.updated_by, b.updated_date, b.bs_mtrl_ex1, b.bs_mtrl_ex2, b.bs_mtrl_ex3, b.bs_mtrl_ex4, b.bs_mtrl_ex5,"
						+ " b.bs_mtrl_ex6, b.bs_mtrl_ex7, b.bs_mtrl_ex8, b.bs_mtrl_ex9, b.prod_model, b.is_mtrl_crs, b.mtrl_ptype, b.tp_id" + " FROM BS_MTRL b " + " WHERE b.is_valid='Y' and partner_code='" + partnerCode + "' and b.org_id='"
						+ orgId + "' and b.org_id='" + orgId + "' and b.mtrl_ptype=" + mtrlPtype + " and b.mtrl_id ='" + mtrlId + "'";
				sql = CommonUtils.addCondition(sql, "b.", "", partnerCode, paramUtils);
				BsMtrlForm bsMtrlForm = sqlQueryUtils.get(sql, new Object[] {}, BsMtrlForm.class);
				if (bsMtrlForm != null) {
					rowData.put("isLed", bsMtrlForm.getIsLed());
					rowData.put("isUseSku", bsMtrlForm.getIsUseSku());
					rowData.put("mtrlName", bsMtrlForm.getMtrlName());
					rowData.put("mtrlDesc", bsMtrlForm.getMtrlDesc());
					rowData.put("mtrlId", bsMtrlForm.getMtrlId());
				} else {
					String fieldName = messageSource.getMessage("column.mtrlcode", locale);
					errMsgArray.add(messageSource.getMessage("common.checkimport.hadnotexisted", new Object[]{i+1,fieldName}, locale));
				}
			}
		} else {
			String fieldName = messageSource.getMessage("column.mtrlcode", locale);
			errMsgArray.add(messageSource.getMessage("common.checkimport.hadnotexisted", new Object[]{i+1,fieldName}, locale));
		}

		// 实际需求数量
		if (!StringUtils.isEmpty((String) rowData.get("tnQty"))) {
			double tnQty = Double.parseDouble((String) rowData.get("tnQty"));
			if (tnQty <= 0) {
				String fieldName = messageSource.getMessage("column.mintnqty", locale);
				errMsgArray.add(messageSource.getMessage("common.checkimport.content", new Object[]{i+1,fieldName}, locale));
			} else {
				// 未完成数量
				rowData.put("auditUncmpQty", (String) rowData.get("tnQty"));
				// 已完成数量
				rowData.put("auditCmpQty", 0 + "");
			}
		}

		// 返回
		Map<String, Object> extendMap = new HashMap<String, Object>();
		extendMap.put("rowData", rowData);
		extendMap.put("errMsgArray", errMsgArray);
		result.setExtend(extendMap);
		return result;
	}

	@Override
	public CheckExcelResult checkCommonData(Map<Integer, Map<String, Object>> rowDatas, Map<Integer, List<String>> errMsgs, ParamUtils paramUtils, JsonRequest<WmsMnForm> extend) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 *    生产其它出库单pdf
	 * @param jsonRequest
	 * @param request
	 * @param paramUtils
	 * @param pdfUrl
	 * @return
	 */
	public EditDataResult<WmsMnForm>  createPdf(JqGridRequest<WmsMnForm> jsonRequest,HttpServletRequest request, ParamUtils paramUtils) {
		Locale locale = paramUtils.getLocale();
		EditDataResult<WmsMnForm> result = new EditDataResult<WmsMnForm>();
		result.setCount(0);
		WmsMnForm data = JSON.parseObject(jsonRequest.getExtend().get("rowData").toString(),WmsMnForm.class);
	    String msg = "";
		
		 if(data==null||StringUtils.isBlank(data.getPmnId())) { //必填值不能为空 
			 msg = messageSource.getMessage("error.column.null", new String[] { }, locale);
			 result.setResultMsg(msg);
			 result.setResultCode(WmsConstants.RESULT_CODE_ERROR); return result;
		 }
//		 
		String ftlPath = FileUploadUtils.getUploadPath(request, "template");//存放模板路径
		if(!new File(ftlPath).exists()) {
			//模板文件不存在，请联系管理员！
			 msg =  messageSource.getMessage("error.column.null", new String[] { }, locale);
			 result.setResultMsg(msg);
			 result.setResultCode(WmsConstants.RESULT_CODE_ERROR); return result;
		}
		
	    String pdfFilepath = FileUploadUtils.getPrintPdfPathName(request,data.getPmnId());
		String ftlName = "mnother1.ftl";
		String pdfName = pdfFilepath.substring(pdfFilepath.indexOf("pdf"+File.separator));
	    String docName = pdfName.substring(0,pdfName.indexOf(".pdf"))+".doc";
	    String docUrl = pdfFilepath.substring(0,pdfFilepath.indexOf("pdf"+File.separator))+docName;
	    //获取模板对应的填充数据数据
		generatorDataMap(data, result, jsonRequest, request, paramUtils);
		if (result.getResultCode().equals(WmsConstants.RESULT_CODE_ERROR)) {
			return result;
		}
		logger.info("生成pdf文档开始.....");
		PdfCreateUtil.ftl2doc(result.getExtend(), ftlPath, ftlName, docUrl, pdfFilepath);
		logger.info("生成pdf文档结束.....");
		result.setCount(1);
		result.setResultCode(WmsConstants.RESULT_CODE_OK);
		result.setResultMsg(FileUploadUtils.getUploadUrl(request, pdfName));
		return result;
	}
	
	/***
	 * 处理数据
	 * @param data
	 * @param result
	 * @param jsonRequest
	 * @param request
	 * @param paramUtils
	 */
	public void generatorDataMap(WmsMnForm data, EditDataResult<WmsMnForm> result, JqGridRequest<WmsMnForm> jsonRequest,
			HttpServletRequest request, ParamUtils paramUtils) {
		result.setResultCode(WmsConstants.RESULT_CODE_ERROR);
		String msg = "";
		// 声明对象
		Locale locale = paramUtils.getLocale();
		LogDto logDto = paramUtils.getLogDto();
		String partnerCode = PartnerCodeHolder.getPartnerCode();
		CheckUtils checkUtils = new CheckUtils(messageSource, parameterService, logDto);
		String pointData = dataRolePointService.getSql(paramUtils.getUserCode(), "WmsMnOther.Query");
		// 必填
		checkUtils.checkIsEmpty("pmnId", data.getPmnId(), locale);
		// 校验数据是否存在
		String sql = "select u4.USER_NAME,w.em_id,dept.dept_name, alias.segment, w.disposition_id, w.pmn_id, w.partner_code, w.org_id, w.pmn_code, w.pmn_type, w.ref_mn_code, w.ref_mn_date, w.prod_id, w.mn_plan_qty, w.mn_tb_qty, w.mn_mp_stat, w.mn_mp_bdate, w.mn_mp_edate, w.cust_id, w.ptm_oppt, w.fl_rule, w.is_bonded, w.vm_store_id, w.mn_audit_stat, w.mn_src_type, w.rcv_type, w.rcv_stat, w.rcv_bdate, w.rcv_edate, w.omtrl_type, w.is_vm_mn, w.vm_mn_id, w.dept_id, w.sup_id, w.mn_io_type, w.inc_store_id, w.tran_cls_id, w.mn_reason, w.is_out_qc, w.ship_date, w.ship_addr, w.post_by, w.post_date, w.audit_by, w.audit_date, w.close_by, w.close_date, w.remark, w.created_by, w.created_date, w.updated_by, w.updated_date, w.wms_mn_ex1, w.wms_mn_ex2, w.wms_mn_ex3, w.wms_mn_ex4, w.wms_mn_ex5, w.wms_mn_ex6, w.wms_mn_ex7, w.wms_mn_ex8, w.wms_mn_ex9  ";
		sql += " ,c.message_key org_name ,u1.user_code post_user_code, u2.user_code audit_user_code, u3.user_code close_user_code ";
		sql += " from WMS_MN w ";
		sql += " left join ims_company c on c.company_code=w.org_id and c.partner_code={partnerCode} ";
		sql += " left join ims_company d on d.company_code=w.dept_id and d.partner_code={partnerCode} ";
		sql += " left join ims_user u1 on u1.user_id=w.post_by ";
		sql += " left join ims_user u2 on u2.user_id=w.audit_by ";
		sql += " left join ims_user u3 on u3.user_id=w.close_by ";
		sql += " left join ims_user u4 on u4.user_id=w.CREATED_BY ";
		sql += " left join bs_dept dept on dept.dept_id = w.dept_id ";
		sql += " left join ims_user emp on emp.user_id = w.em_id ";
		sql += " left join bs_alias alias on alias.disposition_id = w.disposition_id ";
		sql += " where w.pmn_id=? ";
		sql = CommonUtils.addCondition(sql, "w.", pointData, partnerCode, paramUtils);
		WmsMnForm wmsMnForm = sqlQueryUtils.get(sql, new Object[] { data.getPmnId() }, WmsMnForm.class);
		
		// 查询出库LPN
//		sql = " select a.lpn,a.lotno,a.lpn_init_qty,a.lpn_out_stat ";
//		sql += " ,b.mtrl_code,b.mtrl_name,b.mtrl_desc ";
//		sql += " ,c.store_code ";
//		sql += " ,s.sku_code ";
//		sql += " from bs_mn_lpn a,WMS_MN w ";
//		sql += " inner join bs_mtrl b on a.mtrl_id=b.mtrl_id ";
//		sql += " left join bs_store c on a.out_store_id=c.store_id ";
//		sql += " left join bs_mtrl_sku_hd s on s.bs_mtrl_sku_hd_id=a.bs_mtrl_sku_hd_id ";
//		sql += " where a.mn_id=? ";
//		sql += WmsCommonUtils.getSqlByBsMnLpnBetweenOneYearsString("a");
		
//		sql = " select a.lpn,a.lotno,a.lpn_out_stat,b.BS_MTRL_EX1,d.remark ";
//		sql += " ,b.mtrl_code,b.mtrl_name,b.mtrl_desc ";
//		sql += " ,c.store_code ";
//		sql += " ,s.sku_code,sum(a.lpn_init_qty) as lpn_init_qty ";
//		sql += " from bs_mn_lpn a ";
//		sql += " inner join bs_mtrl b on a.mtrl_id=b.mtrl_id ";
//		sql += " left join bs_store c on a.out_store_id=c.store_id ";
//		sql += " left join bs_mn_mtrl d on a.mn_id=d.mn_id and b.mtrl_id=d.mtrl_id and a.lotno=d.lotno";
//		sql += " left join bs_mtrl_sku_hd s on s.bs_mtrl_sku_hd_id=a.bs_mtrl_sku_hd_id ";
//		sql += " where a.mn_id=? ";
//		sql += WmsCommonUtils.getSqlByBsMnLpnBetweenOneYearsString("a");
//		sql += " GROUP BY a.lpn,a.lotno,a.lpn_init_qty,a.lpn_out_stat,b.mtrl_code,b.mtrl_name,b.mtrl_desc,c.store_code,s.sku_code,b.BS_MTRL_EX1,d.remark ";
////		
//		
//		List<BsMnLpnForm> bsMnMtrls = sqlQueryUtils.queryAll(sql, new Object[] { data.getPmnId() }, BsMnLpnForm.class);

		// 查询物料明细
		sql = "select a.bs_mn_mtrl_id,a.mn_id,a.mtrl_id,a.tn_qty,a.audit_uncmp_qty,a.audit_cmp_qty,a.remark,a.updated_date," + "b.mtrl_code,b.mtrl_name,b.mtrl_desc,b.is_led,b.is_use_sku ";
		sql += ",(SELECT CASE WHEN SUM( LPN_QTY ) IS NULL THEN 0 ELSE SUM ( LPN_QTY ) END ";
		sql += " FROM bs_lpn_stock ";
		sql += " WHERE mtrl_id = a.mtrl_id AND lpn_qty > 0 AND qlty_stat = 2 AND is_rc = 'N' AND lpn_lock_type = 0 AND loc_stat=1) stock_count";
		sql += " from bs_mn_mtrl a ";
		sql += " join bs_mtrl b on a.mtrl_id=b.mtrl_id ";
		sql += " where a.mn_id=?";			
		List<BsMnMtrlForm> bsMnMtrlss = sqlQueryUtils.queryAll(sql, new Object[] { data.getPmnId() }, BsMnMtrlForm.class);
		//		result.getExtend().put("mnLpnList", sqlQueryUtils.queryAll(sql, new Object[] { wmsMnForm.getPmnId() }, BsMnLpnForm.class));
//		result.setCount(PangusConstants.RESULT_COUNT_ONE);
//		result.setResultCode(PangusConstants.RESTULT_OK);
//		result.setData(wmsMnForm);
//		if (wmsMnForm == null) {
//			// 其它出库单不存在
//			String name = messageSource.getMessage("table.wmsmnother", locale);
//			msg = messageSource.getMessage("common.notexists", new Object[] { name }, locale);
//			result.setResultMsg(msg);
//			result.setResultCode(WmsConstants.RESULT_CODE_ERROR);
//			return;
//		}

		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("OU_NAME", "深圳市锐明技术股份有限公司");
		dataMap.put("PMN_TYPE", messageSource.getMessage("parameter.PMN_TYPE."+wmsMnForm.getPmnType(), locale));// 单据类型
		
		
		
		dataMap.put("SHIP_DATE", wmsMnForm.getPostDate()==null?"":DateUtils.format(wmsMnForm.getPostDate(), "yyyy-MM-dd"));// 交易日期
		dataMap.put("PMN_CODE", wmsMnForm.getPmnCode());// 编号
		dataMap.put("MN_AUDIT_STAT",messageSource.getMessage("parameter.MN_AUDIT_STAT."+wmsMnForm.getMnAuditStat(), locale));// 单据状态
		dataMap.put("segment", StringUtils.isBlank(wmsMnForm.getSegment())?"":wmsMnForm.getSegment());// 领料部门
		dataMap.put("DEPT_NAME", StringUtils.isBlank(wmsMnForm.getDeptName())?"":wmsMnForm.getDeptName());// 领料部门
		dataMap.put("WM_REMARK", StringUtils.isBlank(wmsMnForm.getRemark())?"":wmsMnForm.getRemark());// 备注

		
		
//		&& bsMnMtrlss != null
//		if (bsMnMtrls.isEmpty() ) {
			// LPN明细不存在
		    List<Map<String, Object>> lists = new ArrayList<>();
			for (int i = 0; i < bsMnMtrlss.size(); i++) {
				BsMnMtrlForm bsmm = bsMnMtrlss.get(i);
				Map<String, Object> dMap = new HashMap<>();
				dMap.put("LN", i+1);// 行号
				dMap.put("MTRL_CODE", dealName(bsmm.getMtrlCode()));// 物料编码
				dMap.put("MTRL_NAME",dealName( bsmm.getMtrlName()));// 物料说明
				dMap.put("MUNIT",dealName(StringUtils.isBlank(bsmm.getMtrlDesc())?"":bsmm.getMtrlDesc()));// 物料规格 单位
				dMap.put("LOTNO", bsmm.getStockCount() == null ? "无数据" : bsmm.getStockCount());// 批号 / 库存数量
//				dMap.put("MUNIT",dealName(StringUtils.isBlank(bsmm.getMtrlDesc())?"":bsmm.getMtrlDesc()));// 物料规格
//				库存数量
//				dMap.put("STOCK_COUNT", bsmm.getStockCount());
				
				dMap.put("TN_QTY", bsmm.getTnQty() == null ? "" : bsmm.getTnQty());// 数量
				dMap.put("STORE_NAME", bsmm.getAuditUncmpQty() == null ? "" : bsmm.getAuditUncmpQty());// 发货仓库 / 未完成数量
//				dMap.put("STORE_NAME", "DZ121");// 发货仓库
				dMap.put("STORELOC_CODE", bsmm.getAuditCmpQty() == null ? "" : bsmm.getAuditCmpQty());// 物料货位 /库存数量
				dMap.put("REMARK", dealName(StringUtils.isBlank(bsmm.getRemark())?"":bsmm.getRemark()));// 备注
				lists.add(dMap);
				dataMap.put("RECIPIENTS", dealName(StringUtils.isBlank(wmsMnForm.getEmId())?"":wmsMnForm.getEmId()));// 领用人
				//dataMap.put("EM_ID", StringUtils.isEmpty(wmsMnForm.getEmId())?"":wmsMnForm.getEmId());// 领用人
				dataMap.put("CREATED_NAME", dealName(wmsMnForm.getUserName())); // 制单
				dataMap.put("list", lists);
			}
//		} else {
//			List<Map<String, Object>> lists = new ArrayList<>();
//			for (int i = 0; i < bsMnMtrls.size(); i++) {
//				BsMnLpnForm bsmm = bsMnMtrls.get(i);
////				BsMnMtrlForm bsmm2 = bsMnMtrlss.get(i);
//				Map<String, Object> dMap = new HashMap<>();
////				如果出库明细的内容为空，就执行物料明细的内容
//				// 循环的表单信息
//				dMap.put("LN", i+1);// 行号
////				dMap.put("MTRL_CODE", bsmm2.getMtrlName());// 物料编码
//				dMap.put("MTRL_CODE", bsmm.getMtrlCode());// 物料编码
//				dMap.put("MTRL_NAME",dealName( bsmm.getMtrlName()));// 物料说明
//				dMap.put("MUNIT",dealName(StringUtils.isBlank(bsmm.getMtrlDesc())?"":bsmm.getMtrlDesc()));// 物料规格 单位
////				dMap.put("MUNIT",dealName(StringUtils.isBlank(bsmm.getMtrlDesc())?"":bsmm.getMtrlDesc()));// 物料规格
////				库存数量
////				dMap.put("STOCK_COUNT", "");
//				dMap.put("LOTNO", StringUtils.isBlank(bsmm.getLotno())?"":bsmm.getLotno());// 批号
//				dMap.put("TN_QTY", bsmm.getLpnInitQty());// 数量
//				dMap.put("STORE_NAME", StringUtils.isBlank(bsmm.getStoreCode())?"":bsmm.getStoreCode());// 发货仓库
////				dMap.put("STORE_NAME", "DZ121");// 发货仓库
//				dMap.put("STORELOC_CODE", "");// 物料货位 /库存数量
//				dMap.put("REMARK", StringUtils.isBlank(bsmm.getRemark())?"":bsmm.getRemark());// 备注	
//				lists.add(dMap);
//				dataMap.put("RECIPIENTS", StringUtils.isBlank(wmsMnForm.getEmId())?"":wmsMnForm.getEmId());// 领用人
//				//dataMap.put("EM_ID", StringUtils.isEmpty(wmsMnForm.getEmId())?"":wmsMnForm.getEmId());// 领用人
//				
//				dataMap.put("CREATED_NAME", wmsMnForm.getUserName()); // 制单
//				dataMap.put("list", lists);
//			}
//		}
		result.setExtend(dataMap);
		result.setResultCode(WmsConstants.RESULT_CODE_OK);

	}
	
	 //替换掉&，不然打印会报错
	public String dealName(String name) {
		if(StringUtils.isNotBlank(name)) {
			name = name.replaceAll("&", "").replaceAll("<", "小于").replaceAll(">", "大于");
			name = name.replaceAll(" ", "");
			name = name.replaceAll("\u0002", "");
		}
		return name;
	}
}